//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;

namespace Eas.WebCrawler.PlugIns.RequestComparers {
    /// <summary>Compares the priority of requests using the sort order of their hash code (psuedo-randomly).</summary>
    /// <remarks>Using the sort order of hash codes for priority is nice because the priority is stable (it does not change across comparisons of the same two objects)
    /// Yet the sort order will be evenly distrubuted and more or less orthoginal to the data in the request.</remarks>
    public class HashCodeComparer : Eas.WebCrawler.PlugIns.RequestComparers.BaseComparer{
        /// <summary>Constructs a <see cref="HashCodeComparer"/>.</summary>
        public HashCodeComparer() { }

        /// <summary>Compares two <see cref="Uri"/>s to see which one comes first in a numeric sorting of the request's hashcodes.</summary>
        /// <param name="leftRequest">The left <see cref="Eas.WebCrawler.Interfaces.Request"/> to compare.</param>
        /// <param name="rightRequest">The right <see cref="Eas.WebCrawler.Interfaces.Request"/> to compare.</param>
        /// <returns>A <see cref="CompareResult"/> declaring if the left or right <see cref="Eas.WebCrawler.Interfaces.Request"/> is higher priority.</returns>
        public override CompareResult CompareUris(Eas.WebCrawler.Interfaces.Request leftRequest, Eas.WebCrawler.Interfaces.Request rightRequest) {
            int leftCode = leftRequest.Uri.GetHashCode();
            int rightCode = rightRequest.Uri.GetHashCode();
            if (leftCode < rightCode)
                return CompareResult.LeftIsHigherPriority;
            else if (leftCode > rightCode)
                return CompareResult.RightIsHigherPriority;
            else
                return CompareResult.PrioritiesAreEqual;
        }

    }
}
